package com.yikaiye.android.yikaiye.ui.activity.login.viewmodel;

import androidx.lifecycle.MutableLiveData;

import com.google.gson.Gson;
import com.yikaiye.android.yikaiye.config.Constant;
import com.yikaiye.android.yikaiye.di.retrofit.BaseModel;
import com.yikaiye.android.yikaiye.di.retrofit.BaseNetCallback;
import com.yikaiye.android.yikaiye.model.UserModel;

import java.util.HashMap;
import java.util.Map;

public class RegisterViewModel extends SendCodeViewModel {

    /**
     * 绑定成功
     */
    public MutableLiveData<UserModel> registerSuccess = new MutableLiveData<>();
    /**
     * 绑定失败
     */
    public MutableLiveData<String> registerError = new MutableLiveData<>();

    /**
     * 手机号
     */
    public MutableLiveData<String> phoneNumber = new MutableLiveData<>();
    /**
     * 密码
     */
    public MutableLiveData<String> password = new MutableLiveData<>();
    /**
     * 验证码
     */
    public MutableLiveData<String> code = new MutableLiveData<>();

    /**
     * 注册
     * type：0密码、2验证码、3是app微信登录、4新用户注册
     */
    public void register(String phone, String verifyCode, String password) {
        Map<String, Object> map = new HashMap<>();
        map.put("type", 4);
        map.put("phone", phone);
        map.put("verifyCode", verifyCode);
        map.put("action", 0);
        map.put("password", password);
        addSubscribe(mRetrofitHelper.login(new Gson().toJson(map)).subscribeWith(new BaseNetCallback<UserModel>() {

            @Override
            public void onSuccess(BaseModel<UserModel> baseModel) {
                if (Constant.SUCCESS == baseModel.getCode()) {
                    if (null != baseModel.getData()) {
                        registerSuccess.setValue(baseModel.getData());
                    }
                } else {
                    registerError.setValue(baseModel.getMsg());
                }
            }

            @Override
            public void onError(String message) {
                super.onError(message);
                registerError.setValue(message);
            }
        }));
    }
}
